package com.koc.robot;

import android.view.MotionEvent;

import com.koc.robot.common.Vector;

public class GameState extends BasicState  {
	
	private Map mMap;
	private Scene mScene;
	private Player mPlayer;
	
	private Vector mLastTouchPos;
	
	private float SMOOTH_ROTAION_ADJUST = Common.PI * 10.0f / 180.0f;
	private float SMOOTH_PITCH_ADJUST = 0.2f;
	
	public GameState()
	{
		mStateTag = State.STATE_GAME;
		
		mScene = new Scene();
		mMap = new Map(mScene);
		mScene.setMap(mMap);
		mScene.setDrawCenter(new Vector(Global.screenwidth / 2, Global.screenheight / 2));
		
		mPlayer = new Player(Global.context, mScene);
		mScene.setPlayer(mPlayer);
	}
	
	public void load()
	{
		mMap.load("06");
		mScene.setRotateCenter(mMap.getMapWidth() / 2 + 0.5f, mMap.getMapHeight() / 2,
				mMap.getMapDepth() / 2 + 0.5f);
		
		float boxWidth = (Global.screenheight + Global.screenwidth) / 2 / 
				((float)Math.sqrt(Math.pow(mMap.getMapWidth(), 2) + Math.pow(mMap.getMapDepth(), 2)) *
						(1 / (float)Math.sqrt(2)));
		mScene.setBlockWidth(boxWidth);
		
		mScene.initMap2d();
		
		mPlayer.init();
		mPlayer.setScale((float)Math.ceil(boxWidth / 50.0f));
		
		mLastTouchPos = new Vector(0, 0);
		Global.main_activity.showControlButtons(true);
	}
	
	@Override
	public void unload()
	{
		Global.main_activity.showControlButtons(false);
	}
	
	public Player getPlayer()
	{
		return mPlayer;
	}
	
	public void update(float dt)
	{
		mScene.update(dt);
		mPlayer.update(dt);
	}
	
	public void render()
	{	
		mScene.render();
		
		//top-view, draw player after all blocks.
		if(mScene.getPitch() == 1.0f)
			mPlayer.render(0.0f);
	}
		
	@Override
	public void onTouched(MotionEvent e)
	{	
		switch (e.getAction())
		{
		case MotionEvent.ACTION_MOVE:
			{
				float deltaX = e.getX() - mLastTouchPos.x;
				float deltaY = e.getY() - mLastTouchPos.y;
				float rotation = mScene.getRotation() - deltaX * Global.ROTATE_SPEED; 
				float pitch = mScene.getPitch() + deltaY * Global.PITCH_SPEED;
				rotation = rotation < 0 ? (rotation + Common.PI_2) : rotation;
				rotation = rotation > Common.PI_2 ? (rotation - Common.PI_2) : rotation;
				pitch = pitch < 0 ? 0 : pitch;
				pitch = pitch > 1 ? 1 : pitch;
				
				mScene.setRotation(rotation);
				mScene.setPitch(pitch);
				mScene.setRotateDrag(true);
			}
			break;
		case MotionEvent.ACTION_UP:
			{
				mScene.setRotateDrag(false);
				float pitch = mScene.getPitch();
				if(pitch < SMOOTH_PITCH_ADJUST)
				{
					for(int i = 0; i < 4; ++i)
					{
						float rotation = mScene.getRotation();
						if((float)Math.abs(Common.PI_05 * i - (rotation - Common.PI_025)) < SMOOTH_ROTAION_ADJUST)
						{
							float smoothRotation = Common.PI_05 * i + Common.PI_025;
							mScene.setSmoothRotation(smoothRotation);
							mScene.setSmoothPitch(0.0f);
						}
					}
				}
				else if(pitch > 1 - SMOOTH_PITCH_ADJUST)
				{
					mScene.setSmoothPitch(1.0f);
				}
			}
			break;
		}
		
		mLastTouchPos.x = e.getX();
		mLastTouchPos.y = e.getY();
	}
	
	public void onButtonClicked(int buttonId)
	{
		mPlayer.onButtonClicked(buttonId);
	}
}
